package com.zhongshan.ga.mobile.resource;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.restlet.ext.json.JsonRepresentation;
import org.restlet.ext.servlet.ServletAdapter;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

public class ImageResource extends ServerResource{
	
	@Get
	public Representation toImage(Representation representation)
			throws IOException {
		representation = post(representation);
		return representation;
	}

	@Post
	public Representation forImage(Representation representation){
		
		BufferedImage bufImg;
		try {
			String other = ImageResource.class.getResource("").getPath();
			System.out.println(other);
			String root = other.substring(other.indexOf("d:"));
			System.out.println(root);
			String path = root.substring(0, root.indexOf("WEB-INF")-1);
			//System.out.println();
			System.out.println(path);
			bufImg = ImageIO.read(representation.getStream());
			System.out.println(bufImg);
			ImageIO.write(bufImg , "png", new File(path+File.separator+"image"+File.separator+"123.png"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new StringRepresentation("头像上传错误！");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new StringRepresentation("头像上传错误！");
		}
		return new StringRepresentation("头像上传成功！");
	}
}
